package leetcode.D300.T222;

import util.TreeNode;

/**
 * @Author: 18362
 * @Create: 2022-09-19 11:38:04 星期一
 */
class Solution {
    public int countNodes(TreeNode root) {
        if (root == null)
            return 0;
        int leftLevel = getLevel(root.left), rightLevel = getLevel(root.right);
        if (leftLevel != rightLevel)
            return countNodes(root.left) + (1 << rightLevel);
        return countNodes(root.right) + (1 << leftLevel);
    }

    private int getLevel(TreeNode root) {
        int level = 0;
        while (root != null) {
            level++;
            root = root.left;
        }
        return level;
    }
}